package 力扣日常刷题.木22一月.第06天0114;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 帅小伙
 * @date 2022/1/14
 * @description
 */
public class Demo07回溯中等电话号码的字母组合 {




    ArrayList<String> ans =  new ArrayList<>();
    Map<Character, String> phoneMap;
    public List<String> letterCombinations(String digits) {
        if(digits.length()==0){
            return ans;
        }
        phoneMap = new HashMap<Character, String>() {{
            put('2', "abc");
            put('3', "def");
            put('4', "ghi");
            put('5', "jkl");
            put('6', "mno");
            put('7', "pqrs");
            put('8', "tuv");
            put('9', "wxyz");
        }};
        dfs(0,digits,new StringBuffer());
        return ans;
    }

    void dfs(int index,String digits,StringBuffer sb) {
        if(index == digits.length()){
            ans.add(sb.toString());
            return;
        }

        for (char c : phoneMap.get(digits.charAt(index)).toCharArray()) {
            sb.append(c);
            dfs(index+1,digits,sb);
            sb.deleteCharAt(sb.length()-1);
        }
    }
}
